









<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>testnguice - Introduction</title>
    <link rel="stylesheet" href="./css/maven-theme.css" type="text/css" />
    <link rel="stylesheet" href="./css/maven-base.css" type="text/css"/>
    <link rel="stylesheet" href="./css/prettify.css" type="text/css"/>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
    <script type="text/javascript" src="./js/prettify.js"></script>
    <script type="text/javascript" src="./js/niftycube.js"></script>
    <script type="text/javascript">
        window.onload=function() {
            Nifty("div\#wrapper", "big");
            Nifty("div#breadcrumbs", "small");
            Nifty("div\#navcolumn", "small");
            Nifty("div.source", "medium");
            prettyPrint();
        }
    </script>
          <meta name="author" content="Simone Tripodi" />
          </head>
  <body>
    <div id="wrapper">
        <div id="banner">
                        <div id="bannerLeft">
    
            TestNGuice
    
            </div>
                            <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="breadcrumbs">
              
  

  
    
    
  
    
            <div class="xleft">
        <span id="publishDate">Last Published: 2009-11-24</span>
                  &nbsp;| <span id="projectVersion">Version: 2.0</span>
                      </div>
                <div class="xright">      
  

  
    
    
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="leftColumn">
          <div id="navcolumn">
               
  

  
    
    
  
    
                       <h5>Injectlet</h5>
            <ul>
              
    <li class="none">
                    <a href="index.html">Introduction</a>
          </li>
              
    <li class="none">
                    <a href="inject.html">Injection and TestCases</a>
          </li>
              
    <li class="none">
              <strong>Data Providers</strong>
        </li>
          </ul>
              <h5>Project Documentation</h5>
            <ul>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-info.html">Project Information</a>
                </li>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-reports.html">Project Reports</a>
                </li>
          </ul>
              <h5>External Links</h5>
            <ul>
              
    <li class="none">
                    <a href="../testnguice">Project Page</a>
          </li>
              
    <li class="none">
                    <a href="../testnguice/downloads/list">Downloads</a>
          </li>
              
    <li class="none">
                    <a href="http://testng.org" class="externalLink">TestNG</a>
          </li>
              
    <li class="none">
                    <a href="http://testng.org/doc/book.html" class="externalLink">TEH TestNG Book</a>
          </li>
              
    <li class="none">
                    <a href="../google-guice">Google Guice</a>
          </li>
              
    <li class="none">
                    <a href="http://opencsv.sourceforge.net/" class="externalLink">OpenCSV</a>
          </li>
          </ul>
                                               <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img class="poweredBy"
                 alt="Built by Maven"
                 src="./images/logos/maven-feather.png"
                 width="90"
                 height="30"
                 border="1" />
          </a>
                           
  

  
    
    
  
    
            </div>
        </div>
        <div id="bodyColumn">
          <div id="contentBox">
            <div class="section"><h2>Data Providers</h2>
<p>A Data Provider serves two purposes:</p>
<ul><li>Pass an arbitrary number of parametrs to a test method;</li>
<li>Test methods will be invoked many times with different data sets.</li>
</ul>
</div>
<div class="section"><h2>XML based Data Provider</h2>
<p>This data provider is a basic Provider that reads data set from a simple XML file, let's explain how to use it through an basic example.</p>
<p>Given the follow fake test case:</p>
<div class="source"><pre>package org.mycompany.myapp;

class MyTestCase {

    public void verifyTypeCorrectnessTypeConversion(final Date date, final String message, final int integer) {
        ...
    }

}</pre>
</div>
<p>that needs the XML Data Provider, first you need to annotate the test method:</p>
<div class="source"><pre>@org.testng.annotations.Test(
    dataProvider = com.testnguice.providers.XMLDataProvider.DATA_PROVIDER_ID,
    dataProviderClass = com.testnguice.providers.XMLDataProvider.class
)
public void verifyTypeCorrectnessTypeConversion(final Date date, final String message, final int integer) {
    ...
}</pre>
</div>
<p>The Data Provider will read data to send to the method from an XML <i>classpath</i> resource, which location pattern is very easy:</p>
<div class="source"><pre>${qualifiedClassName}.${testMethodName}.test-data.xml</pre>
</div>
<p>So, contextualized to the example, for the method</p>
<div class="source"><pre>org.mycompany.myapp.MyTestCase#verifyTypeCorrectnessTypeConversion</pre>
</div>
<p>The relative <i>classpath</i> resource is</p>
<div class="source"><pre>org/mycompany/myapp/MyTestCase.verifyTypeCorrectnessTypeConversion.test-data.xml</pre>
</div>
<p>Let's speak about the simple XML format of the file: the structure is an <tt>xml-dataprovider</tt> that contains a list of <tt>test</tt> elements that contains a list of <tt>argument</tt>; each <tt>argument</tt> element reflects the natural order of the test method arguments, additional arguments will be ignored.</p>
<p>Users can use the <a href="http://testnguice.googlecode.com/svn/schemas/2.0/xml-dataprovider.xsd" class="externalLink">schema</a> uploaded on testnguice server to build their own data sources, please reference that location with the follow namespace:</p>
<div class="source"><pre>xmlns:xdp=&quot;http://testnguice.com/schemas/2.0/xml-dataprovider&quot;</pre>
</div>
<p>Follows a brief example:</p>
<div class="source"><pre>&lt;xdp:xml-dataprovider xmlns:xdp=&quot;http://testnguice.com/schemas/2.0/xml-dataprovider&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://testnguice.com/schemas/2.0/xml-dataprovider
    http://testnguice.googlecode.com/svn/schemas/2.0/xml-dataprovider.xsd&quot;&gt;
    ...
    &lt;xdp:test&gt;
        &lt;xdp:argument&gt;2009-11-03T12:00:30&lt;/xdp:argument&gt;
        &lt;xdp:argument&gt;another string&lt;/xdp:argument&gt;
        &lt;xdp:argument&gt;17&lt;/xdp:argument&gt;
    &lt;/xdp:test&gt;
    ...
&lt;/xdp:xml-dataprovider&gt;</pre>
</div>
<p><b>Warning</b> The resource classpath will be parsed with a non-validating parser to speed up the test, so please make sure you wrote/generated the data source in the right syntax.</p>
<p>The 'String to Java' objects binding in charge of the apache commons-beanutils <a href="http://commons.apache.org/beanutils/v1.8.2/apidocs/org/apache/commons/beanutils/package-summary.html#conversion" class="externalLink">converter</a> package, so feel free to register you favorite converter:</p>
<div class="source"><pre>import java.util.Date;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

...

DateConverter dateConverter = new DateConverter();
dateConverter.setPatterns(new String[] {
        &quot;yyyy&quot;,
        &quot;yyyy-MM&quot;,
        &quot;yyyy-MM-dd&quot;,
        &quot;yyyy-MM-dd'T'HH&quot;,
        &quot;yyyy-MM-dd'T'HH:mm&quot;,
        &quot;yyyy-MM-dd'T'HH:mm:ss&quot;
});
ConvertUtils.register(dateConverter, Date.class);</pre>
</div>
<p>and then execute your tests, the XML provider will resolve it on run-time and bind the Object.</p>
</div>

          </div>
        </div>
        <div class="clear">
          <hr/>
        </div>
        <div id="footer">
          <div class="xright">Copyright &#169;         
   
          2009.
    
      All Rights Reserved.    
  

  
    
    
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
    </div>
  </body>
</html>
